/**   
 * @Title: SysRole.java 
 * @Description: TODO
 * @date 2011-5-26 上午10:57:07   
 * @version 1.0  
 */
package com.psammead.dms.system.sercurity.entity;     

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.psammead.dms.common.core.entity.RemovableEntity;



/**   
 * @Title： 系统角色
 * @author <a href="mailto:si-qi.liang@hp.com">Liang, Si-Qi</a>  
 * @version 1.0   
 */
@Entity(name = "SysRole")
@Table(name = "tb_sys_role")
public class SysRole extends RemovableEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2144343677934742301L;

	/**
	 * static values of role types;
	 */
	public static final String DEFAULT_ROLE = "D";
	public static final String SPECIAL_ROLE = "S";
	
	private long id;
	private String roleName;
	private String type="D";
	private String description;
	private Set<SysGroup> groups =new HashSet<SysGroup>();
	private Set<SysPermission> permissions =new HashSet<SysPermission>();
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="role_id")
	@Override
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	
	@Column(name="role_name", length=32)
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Column(name="role_type", length=32)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	@Column(name="description", length=100)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * 获取拥有该系统角色的系统群组
	 * @Title:getGroups
	 * @Desciption:多对多关联SysGroup，SysRole作为被控端
	 * @return
	 */
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},mappedBy="roles", fetch = FetchType.LAZY)
	public Set<SysGroup> getGroups() {
		return groups;
	}
	public void setGroups(Set<SysGroup> groups) {
		this.groups = groups;
	}
	
	/**
	 * 获取该角色的权限
	 * @Title: getPermissions
	 * @Description: 多对多关联SysPermission, SysRole作为控制端
	 * @param
	 * @return Set<SysPermission>
	 * @throws	
	 * @return
	 */
	@ManyToMany(cascade = CascadeType.REMOVE)
	@JoinTable(name = "tr_sys_role_permission", 
			joinColumns = {@JoinColumn(name = "role_id",nullable = false)}, 
			inverseJoinColumns = @JoinColumn(name = "permission_id",nullable = false))
	public Set<SysPermission> getPermissions() {
		return permissions;
	}
	public void setPermissions(Set<SysPermission> permissions) {
		this.permissions = permissions;
	}

	public void addPermission(SysPermission permission){
		if(!this.permissions.contains(permission)){
			this.permissions.add(permission);
		}
	}
	public void removePermission(SysPermission permission){
		this.permissions.remove(permission);
	}

}
 